package com.edee.bigday.commodity.app;

import com.edee.bigday.commodity.entity.CategoryEntity;
import com.edee.bigday.commodity.service.CategoryService;
import com.edee.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 商品三级分类
 *
 * @author edee
 * @email 421763094@qq.com
 * @date 2020-12-09 23:08:51
 */
@RestController
@RequestMapping("commodity/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查出所有分类及其子分类，以树形结构组装起来
     */
    @RequestMapping("/list/tree")
    //@RequiresPermissions("commodity:category:list")
    public R list() {
//        PageUtils page = categoryService.queryPage(params);
        List<CategoryEntity> entities = categoryService.listWithTree();
        return R.ok().put("data", entities);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("commodity:category:info")
    public R info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category); //由category修改为data，2020.12.29 01:50
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("commodity:category:save")
    public R save(@RequestBody CategoryEntity category) {
        categoryService.save(category);

        return R.ok();
    }

    /**
     * 批量修改
     *
     * @param category
     * @return
     */
    @RequestMapping("/update/sort")
    //@RequiresPermissions("commodity:category:update")
    public R updateSort(@RequestBody CategoryEntity[] category) {
        //前端将一个数组的json发送过来，SpringMVC自动将其封装为CategoryEntity的数组：
        categoryService.updateBatchById(Arrays.asList(category));//updateBatchById是一个批量修改的方法
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("commodity:category:update")
    public R update(@RequestBody CategoryEntity category) {
//        categoryService.updateById(category);
        //将原生方法修改为如下方法：
        categoryService.updateCascade(category);

        return R.ok();
    }

    /**
     * 删除
     *
     * @RequestBody：获取请求体，必须发送POST请求。 SpringMVC自动将请求体的数据(json)转为对应的对象，如，下面long[]类型的数组catIds。
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("commodity:category:delete")
    public R delete(@RequestBody Long[] catIds) {
        //1、检查需要删除的菜单有没有被引用：
//		categoryService.removeByIds(Arrays.asList(catIds));
        categoryService.removeMenuByIds(Arrays.asList(catIds));

        return R.ok();
    }

}